<template>
	<view class="home-container" :style="{ 'margin-top': '50px'}">
		<!-- 轮播图区域 -->
		 <swiper :indicator-dots="true" 
		      indicator-active-color="#ffffff" 
			  :autoplay="true" 
			  :interval="3000" 
			  :duration="2000" 
			  :circular="true">
		      <!-- 循环渲染轮播图的 item 项 -->
		      <swiper-item v-for="(item, i) in swiperList" :key="i">
		        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
		          <!-- 动态绑定图片的 src 属性 -->
		          <image :src="item.image_src"></image>
		        </navigator>
		      </swiper-item>
		</swiper>
		<!-- 分类导航区域 -->
		<view class="nav">
			<view class="nav-item" v-for="(nav,index) in navList" :key="index" @click="navClickHandle(nav)">
				<image :src="nav.image_src" mode="" class="nav-image"></image>
			</view>
		</view>
		<!-- 楼层标题 -->
		<view class="floor">
			<view class="floor-item" v-for="(floor,index) in floorList" :key="index">
				<image :src="floor.floor_title.image_src" class="floor-image"></image>
				<view class="floor-img-box">
					<!-- // 左侧楼层大图 -->
					<navigator class="left-img-box" :url="floor.product_list[0].url">
						<image :src="floor.product_list[0].image_src" :style="{width: floor.product_list[0].image_width + 'rpx' }" mode="widthFix"></image>
					</navigator>
					<!-- // 右侧楼层小图 -->
					<view class="right-img-box">
						<navigator class="right-item-img" v-for="(floorImage,imgIndex) in floor.product_list.slice(1)" :url="floorImage.url" :key="imgIndex">
							<image  mode="widthFix" :src="floorImage.image_src"></image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
		<!-- 使用自定义的搜索组件 -->
		<my-search @click="gotoSearch"></my-search>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	import MySearch from '@/components/my-search/my-search.vue'
	export default {
		mixins: [badgeMix],
		components:{
			MySearch
		},
		data() {
			return {
				swiperList:[], //banner数据
				navList: [],  //导航数据
				floorList: [], //楼层数据
				marginTop: 0
			}
		},
		onLoad () {
			// if (uni.getSystemInfoSync().platform === 'devtools') {
			// 	this.marginTop = 50
			// }
			uni.getSystemInfo({
				success (info) {
					console.log(info)
					if (info.brand === "Xiaomi") {
						this.marginTop = 50
					}
				}
			})
		},
		mounted () {
			console.log('onload')
			// 获取首页banner
			this.getSwiperList()
			// 获取首页导航
			this.getNavList()
			// 获取楼层数据
			this.getFloorList()
		},
		methods: {
			async getSwiperList () {
				const {data: res} = await uni.$http.get('/api/public/v1/home/swiperdata')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.swiperList = res.message
			},
			async getNavList () {
				const {data:res} = await uni.$http.get('/api/public/v1/home/catitems')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.navList = res.message
			},
			// 分类跳转
			navClickHandle (nav) {
				// uni.switchTab({
				// 	url:'/pages/cate/cate'
				// })
				if (nav.name === "分类") {
					uni.switchTab({
						url:'/pages/cate/cate'
					})
				}
			},
			async getFloorList () {
				const { data: res } = await uni.$http.get('/api/public/v1/home/floordata')
				if (res.meta.status !== 200) return uni.$showMsg()
				res.message.forEach((floor) => {
					floor.product_list.forEach(prod  => {
						prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
					})
				})
				this.floorList = res.message
			},
			gotoSearch () {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.home-container{
		// margin-top: 50px;
		
		swiper {
			height: 330rpx;
			.swiper-item,image{
				height: 100%;
				width: 100%;
			}
		}
		.nav{
			display: flex;
			justify-content: space-around;
			align-items: center;
			margin: 15px 0;
			.nav-image {
				width: 128rpx;
				height: 140rpx;
			}
		}
		.floor-image {
			height: 60rpx;
			width: 100%;
			display: flex;
		}
		.floor-img-box{
			display: flex;
			padding-left: 10rpx;
			.right-img-box{
				display: flex;
				flex-wrap: wrap;
				justify-content: space-around;
				.right-item-img{
					width: 232rpx;
					image{
					  width: 100%;
					  height: 100%;
					}
				}
			}
		}
	}
</style>
